//
//  EMOrderInformationViewController.m
//  EasyMoney
//
//  Created by zh on 2019/12/14.
//  Copyright © 2019 zh. All rights reserved.
//

#import "EMRepaymentViewController.h"
#import "EMNavBarView.h"
#import "EMRepaymentHeaderView.h"
#import "EMRepayProgressView.h"
#import "EMRepayBottomItemView.h"
#import "EMRepaymentRequestModel.h"
#import "EMRepaymentResponseModel.h"
#import <MJRefresh.h>
#import "EMLocationManager.h"

@interface EMRepaymentViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) EMRepaymentHeaderView *headerView;
@property (nonatomic, strong) EMRepayProgressView *progressView;
@property (nonatomic, strong) EMRepayBottomItemView *bottomView1; //第一个按钮
@property (nonatomic, strong) EMRepayBottomItemView *bottomView2; //第二个按钮
@property (nonatomic, strong) EMRepaymentResponseModel *dataModel; //数据源

@end

@implementation EMRepaymentViewController

- (void)dealloc{
    LOG_MARK;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    //获取localData
    EMRepaymentResponseModel *model = (EMRepaymentResponseModel *)[[EMCacheMananger shared]getLocalData:@"Repayment"];
    if (model) {
        self.dataModel = model;
    }
    [self requestData];
    [[EMLocationManager sharedManager] start];
}


- (void)setupUI{

    self.view.backgroundColor = [UIColor whiteColor];
    EMNavBarView *navBarView = [[EMNavBarView alloc]init];
    navBarView.title = NSLocalizedString(@"我的贷款", nil);
    [self.view addSubview:navBarView];
    [navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(EMNavBarHeight);
    }];

    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.directionalLockEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
//    self.scrollView.bounces = NO;
    self.scrollView.backgroundColor = ColorFromRGB(0xf8f8f8);
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(EMStatusBarHeight+44);
        make.left.right.bottom.equalTo(self.view);
    }];
    EMRefreshHeader *header = [[EMRefreshHeader alloc] init];
    [header setRefreshingTarget:self refreshingAction:@selector(beginRefreshHeader)];
    self.scrollView.mj_header = header;
    
    self.headerView = [[EMRepaymentHeaderView alloc]initWithFrame:CGRectZero];
    [self.scrollView addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.scrollView);
        make.height.mas_equalTo(126);
    }];

    self.progressView = [[EMRepayProgressView alloc]initWithFrame:CGRectZero];
    [self.scrollView addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.scrollView);
        make.top.equalTo(self.headerView.mas_bottom).offset(0);
        make.height.mas_equalTo(300);
    }];

    self.bottomView1 = [[EMRepayBottomItemView alloc]initWithFrame:CGRectZero];
    self.bottomView1.contentBtn.tag = 1;
    [self.bottomView1.contentBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.bottomView1];
    [self.bottomView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressView.mas_bottom).offset(30);
        make.left.right.equalTo(self.scrollView);
        make.height.mas_equalTo(50);
    }];

    self.bottomView2 = [[EMRepayBottomItemView alloc]initWithFrame:CGRectZero];
    self.bottomView2.contentBtn.tag = 100;
    [self.bottomView2.contentBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.bottomView2];
    [self.bottomView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bottomView1.mas_bottom).offset(24);
        make.left.right.equalTo(self.scrollView);
        make.height.mas_equalTo(50);
    }];
    
}

- (void)requestData{
    EMRepaymentRequestModel *request = [[EMRepaymentRequestModel alloc]init];
    [request requestSuccess:^(EMRepaymentResponseModel *response, NSString * _Nonnull message) {
        [self.scrollView.mj_header endRefreshing];
        self.dataModel = response;
        self.headerView.repayMoney = response.unrepayLoanAmt;
        self.progressView.model = response;
        self.bottomView1.title = response.button1text;
        self.bottomView2.title = response.button2text;
        [[EMCacheMananger shared]updateLocalData:response andModelName:@"Repayment"];
    } failure:^(NSInteger errorCode, NSString * _Nonnull errorMessage, NSError * _Nonnull error) {
        [self.scrollView.mj_header endRefreshing];
    }];
}

- (void)beginRefreshHeader
{
    [self requestData];
}

#pragma mark - private
- (void)clickBottomView:(UIButton *)sender{
    NSInteger tag = sender.tag;
    if (tag == 1) {
        ICEOpenUrl(self.dataModel.button1url);
    }else if (tag == 100){
        //点击第二个按钮
        ICEOpenUrl(self.dataModel.button2url);
    }else{
        return;
    }
}
@end
